﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace DotNetNeural.Test.Helpers
{
    public class MatrixHelper
    {
        public static float[,] Parse(string str)
        {
            string[] rows = str.Split(rowSeparator);

            List<float[]> list = new List<float[]>();

            for (int i = 0; i < rows.Length; ++i)
            {
                string[] cols = rows[i].Split(colSeparator);

                float[] fRow = new float[cols.Length];
                for (int j = 0; j < cols.Length; ++j)
                {
                    fRow[j] = Single.Parse(cols[j], CultureInfo.InvariantCulture.NumberFormat);
                }

                list.Add(fRow);
            }

            int maxCol = list.Select(f => f.Length).Max();

            float[,] result = new float[rows.Length, maxCol];
            for (int i = 0; i < rows.Length; ++i)
            {
                for (int j = 0; j < maxCol; ++j)
                {
                    result[i, j] = list[i][j];
                }
            }

            return result;
        }

        private static char[] rowSeparator = new char[] { ';' };
        private static char[] colSeparator = new char[] { ',' };
    }
}
